package com.iwanghang.sqlitedemo.dialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.iwanghang.sqlitedemo.Person;
import com.iwanghang.sqlitedemo.R;

/**
 * 插入对话框
 */
public class InsertDialog extends Activity implements View.OnClickListener {
    /**
     * 插入请求代码 大于0
     */
    private static final int INSERT_REQUESTCODE = 1;
    /**
     * 姓名控件
     */
    private EditText nameView;
    /**
     * 年龄控件
     */
    private EditText ageView;
    /**
     * 性别控件
     */
    private RadioGroup sexView;
    private RadioButton mMale;
    private RadioButton mFemale;


    private String sexName;

    private int intAge;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);    //去掉标题栏
        setContentView(R.layout.insert_dialog);

        init();
    }

    private void init() {
        findViewById(R.id.insert_confirm).setOnClickListener(this);
        findViewById(R.id.insert_cancel).setOnClickListener(this);
        nameView = (EditText) findViewById(R.id.insert_edit_name);

        ageView = (EditText) findViewById(R.id.insert_edit_age);
        sexView = (RadioGroup) findViewById(R.id.insert_edit_sex);


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.insert_confirm:
                confirm(); // 确认插入
                break;
            case R.id.insert_cancel:
                cancel(); // 取消插入
                break;
        }
    }

    /**
     * 确认插入
     */
    private void confirm() {

        //姓名
        String name = String.valueOf(nameView.getText());
        if (name.toString().length() == 0) {

            Toast.makeText(InsertDialog.this, "请输入姓名", Toast.LENGTH_LONG).show();
            return;

        }

        //年龄
        String age = String.valueOf(ageView.getText());
        if (age.toString().length() == 0) {
            Toast.makeText(InsertDialog.this, "请输入年龄", Toast.LENGTH_LONG).show();
        } else {
            intAge = Integer.valueOf(String.valueOf(ageView.getText()));

        }
        // 性别

        sexView = (RadioGroup) super.findViewById(R.id.insert_edit_sex);
        RadioButton radioButton = (RadioButton)findViewById(sexView.getCheckedRadioButtonId());



        if( null == radioButton) {

            Toast.makeText(InsertDialog.this, "请选择性别", Toast.LENGTH_LONG).show();
            return;

        }else{
            sexName=radioButton.getText().toString();

        }

        Person person = new Person();
        person.setName(name);
        person.setAge(intAge);
        //sexName = sexView.getCheckedRadioButtonId() == R.id.mMale ? "男" : "女";

        person.setSex(sexName);
        Intent intent = new Intent();
        intent.putExtra("person", person);
        setResult(INSERT_REQUESTCODE, intent);
        finish();
    }

    /**
     * 取消插入
     */
    private void cancel() {
        setResult(INSERT_REQUESTCODE);
        finish();
    }
}
